/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.UserBean;
import ejb.UserBeanLocal;
import entities.Purchase;
import entities.User;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import utils.UtilsBean;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@RequestScoped
public class UserManagedBean {
private List<String> cancelAcount;
    @EJB
    private UserBeanLocal userBean;
    private User user;
    private String passActual;
    private String pass1;
    private String pass2;

    public UserManagedBean() {
        this.cancelAcount= new ArrayList<String>();
        this.userBean = new UserBean();
        this.user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.ACTUAL_USER);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<String> getCancelAcount() {
        return cancelAcount;
    }

    public void setCancelAcount(List<String> cancelAcount) {
        this.cancelAcount = cancelAcount;
    }

    public String saveProfile() {
        Boolean fatalError = false;
        if (UtilsBean.sha512(passActual).equals(user.getPass())) {
            if (user.getEmail() == null || user.getEmail().isEmpty()) {
                fatalError = true;
            }
            if (user.getName() == null || user.getName().isEmpty()) {
                fatalError = true;
            }
            if (user.getSurname() == null || user.getSurname().isEmpty()) {
                fatalError = true;
            }
            if (user.getPhone() == null || user.getPhone().isEmpty()) {
                fatalError = true;
            }


            if (fatalError) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error no esperado"));
            } else {


                Boolean error = false;
                Boolean isNeedValidPass = pass1 != null && !pass1.isEmpty();
                if (isNeedValidPass) {
                    error = pass1 != null && !pass1.isEmpty() && pass2 != null && !pass2.isEmpty() && !pass1.equals("") && !pass1.equals(pass2) && pass1.length() > 6 && pass1.length() < 15;
                }


                if (error) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Las contraseñas deben coincidir y ser superior a 6 carácteres e inferior a 15", "Escribalas de nuevo"));
                } else {
                    if (pass1 != null && !pass1.isEmpty()) {
                        user.setPass(UtilsBean.sha512(pass1));
                    }

//                    user.setName(pass1);
//                    user.setSurname(pass1);
//                    user.setAddress(pass1);
//                    user.setPhone(pass1);

                   
                    // FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.ACTUAL_USER, user);
                    if(!cancelAcount.isEmpty()){
                        user.setStatus(0);
                        userBean.addUser(user);
                        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
                        return "LOGOUT";
                    }else{
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El usuario se ha modificado correctamente", ""));
                    userBean.addUser(user);
                    }
                    
                }
            }
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "La contraseña actual no es correcta", "Escribala de nuevo"));

        }

        return "UPDATE";
    }

    public UserBeanLocal getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBeanLocal userBean) {
        this.userBean = userBean;
    }

    public String getPass1() {
        return pass1;
    }

    public void setPass1(String pass1) {
        this.pass1 = pass1;
    }

    public String getPass2() {
        return pass2;
    }

    public void setPass2(String pass2) {
        this.pass2 = pass2;
    }

    public String getPassActual() {
        return passActual;
    }

    public void setPassActual(String passActual) {
        this.passActual = passActual;
    }
}
